package org.elanfox.intellicommunity.msg.server.service.impl;

import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.util.HexRead;
import org.elanfox.intellicommunity.common.utils.HexUtils;
import org.elanfox.intellicommunity.msg.dto.CmdWriteParam;
import org.elanfox.intellicommunity.msg.ienum.CmdEnums;
import org.elanfox.intellicommunity.msg.server.base.MsgChannelRegister;
import org.elanfox.intellicommunity.msg.server.base.MsgCommunicationUtils;
import org.elanfox.intellicommunity.msg.server.base.WriteResult;
import org.elanfox.intellicommunity.msg.server.service.CmdNormalWriter;
import org.springframework.stereotype.Service;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/3/17
 */
@Service
@Slf4j
public class CmdNormalWriterImpl implements CmdNormalWriter {
    @Override
    public void timeVerify(CmdWriteParam cmdWriteParam, Channel channel) {
        String hexLength = "000A";
        String hexData = HexUtils.numToHex32(System.currentTimeMillis() / 1000);


        String requestData = MsgCommunicationUtils.buildRequest(cmdWriteParam.getHexAddr(), CmdEnums.Code.TIME_VERIFY, hexData, hexLength);
//        System.out.println("time req===>" + requestData);

        if (!MsgChannelRegister.isExist(cmdWriteParam.getHexAddr())) {
            channel.writeAndFlush(Unpooled.copiedBuffer(HexRead.readFromString(requestData)));
            //保存channel
            MsgChannelRegister.register(cmdWriteParam.getHexAddr(), channel);
        }
    }

    @Override
    public WriteResult write(CmdEnums.Code cmdCode, CmdWriteParam arg) {
        return null;
    }
}
